using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;

namespace IReaper.VideoPlay
{
    public partial class PlayListManageMenu : MenuStrip
    {
        private PlayListForm _contextForm;

        public ToolStripMenuItem OpenListMenuItem
        {
            get
            {
                return this.openListMenuItem;
            }
        }

        public ToolStripMenuItem PlayNowItem
        {
            get
            {
                return this.PlayNowMenuItem;
            }
        }

        public PlayListManageMenu(PlayListForm ctx)
        {
            _contextForm = ctx;
            InitializeComponent();
        }

        public PlayListManageMenu(IContainer container, PlayListForm ctx)
        {
            container.Add(this);
            _contextForm = ctx;

            InitializeComponent();
        }        

        void saveMenuItem_Click(object sender, System.EventArgs e)
        {
            FileStream fs = new FileStream(((PlayListForm)_contextForm).CurrentPlayListFile, FileMode.Create, FileAccess.ReadWrite);
            PlayList list = Core.CoreData[CoreDataType.CurrentPlayList] as PlayList;
            if (list != null)
                VideoPlayHelper.Instance.SavePlayListFile(fs, list);
        }

        void otherMenuItem_Click(object sender, System.EventArgs e)
        {
            CreateNewPlayListWizard newListWizard = new CreateNewPlayListWizard();
            newListWizard.ShowDialog();
        }

        void loadFileMenuItem_Click(object sender, System.EventArgs e)
        {
            OpenFileDialog dia = new OpenFileDialog();
            dia.Filter = "VideoList files (*.vlist)|*.vlist";
            if (dia.ShowDialog() == DialogResult.OK)
            {
                //string name = dia.FileName.Substring(0, dia.FileName.Length - 1 - dia.FileName.IndexOf("."));                
                string name = GetFinalFileName(dia.FileName);
                VideoPlayHelper.Instance.RegPlayList(name, dia.FileName);
            }
        }

        void saveAsMenuItem_Click(object sender, System.EventArgs e)
        {
            SaveFileDialog dia = new SaveFileDialog();
            dia.Filter = "VideoList files (*.vlist)|*.vlist";
            dia.DefaultExt = "vlist";
            dia.AddExtension = true;
            if (dia.ShowDialog() == DialogResult.OK)
            {
                PlayList list = Core.CoreData[CoreDataType.CurrentPlayList] as PlayList;
                VideoPlayHelper.Instance.SavePlayListFile(dia.FileName, list);
                string name = GetFinalFileName(dia.FileName);
                VideoPlayHelper.Instance.RegPlayList(name, dia.FileName);
            }
        }

        private string GetFinalFileName(string fileName)
        {
            FileInfo fi = new FileInfo(fileName);
            string name = fi.Name.Substring(0, fi.Name.IndexOf("."));
            if (VideoPlayHelper.Instance.Lists.IndexOfKey(name) != -1)
            {
                Random ra = new Random();
                name = name + ra.Next(100, 999).ToString();
            }
            return name;
        }
    }
}
